package com.zidian.study.adapter;

import java.util.List;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.zidian.study.other.SimpleMenu;


/**
 * @{# SlidePagerApplyAdapter.java Create on 2015-10-21 下午7:25:10
 * <p>
 * class desc: 轮播
 * Copyright: Copyright(c) 2015
 * </p>
 * @Version 1.0   Panca  QQ643691522
 */
public class SlidePagerApplyAdapter extends PagerAdapter {
    private Activity context;
    private List<SimpleMenu> slideItems;

    public SlidePagerApplyAdapter(Activity context, List<SimpleMenu> slideItems) {
        super();
        this.context = context;
        this.slideItems = slideItems;
    }

    @Override
    public int getCount() {
        if (slideItems != null) {
            return slideItems.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView image = new ImageView(context);
        image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        image.setAdjustViewBounds(true);
        image.setScaleType(ScaleType.CENTER_CROP);
        SimpleMenu slideItem = slideItems.get(position);

        try {
            //图片加载
            image.setImageResource(slideItem.getImageRes());
            //YMImageLoadUtil.loadImageWithImg(slideItem.getImageURL(), image, R.drawable.img_defualt);
        } catch (Exception e) {
            // TODO: handle exception
        }

        container.addView(image);
        image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (position) {
                    case 0:
                        //
                        break;
                    case 1:
                        //
                        break;
                    case 2:
                        //
                        break;
                    default:
                        break;
                }
            }
        });
        return image;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}